package com.rockwell.sniffyHunter.test;

import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;
import android.annotation.SuppressLint;

import com.rockwell.sniffyhunter.model.LeftFirstMazeSolver;
import com.rockwell.sniffyhunter.model.Robot;
import com.rockwell.sniffyhunter.model.Unloader;
import com.rockwell.sniffyhunter.utils.DetectionIndex;
import com.rockwell.sniffyhunter.utils.IDetector;
import com.rockwell.sniffyhunter.utils.IMazeSolver;
import com.rockwell.sniffyhunter.utils.IRobotCommander;
import com.rockwell.sniffyhunter.utils.UnloaderState;

public class UnloaderStateTester extends UnitTestLooper {
	private FakeRobotSensors 		sensors;
	private Map<Integer, IDetector>	colorDetectors;
	private FakeColorDetector		maze3Detector;
	private FakeTreasureCollector 	treasureCollector;
	private IRobotCommander 		robotCommander;
	private Robot 					robot;		
	private Unloader 				unloader;
	IMazeSolver 					solver;
	
	@SuppressLint("UseSparseArrays") @SuppressWarnings("serial")
	@Override
	public void setUp() {
		
		solver 				= new LeftFirstMazeSolver();
		sensors 			= new FakeRobotSensors();
		maze3Detector		= new FakeColorDetector();
		colorDetectors 		= new HashMap<Integer, IDetector>() {
							  {
								  put(DetectionIndex.TREASURE, new FakeTreasureDetector());
								  put(DetectionIndex.OBSTACLE, new FakeColorDetector()/*obstacle detector*/); 
								  put(DetectionIndex.MAZE1, new FakeColorDetector()/*maze1 detector*/);
								  put(DetectionIndex.MAZE2, new FakeColorDetector()/*maze2 detector*/);
								  put(DetectionIndex.MAZE3, maze3Detector/*maze3 detector*/);
								  put(DetectionIndex.DROPZONE, new FakeColorDetector()/*dropZone detector*/);
								  put(DetectionIndex.MAZE_EXIT, new FakeColorDetector()/*dropZone detector*/);
							  }};
		robotCommander 		= new DummyRobotCommander();
		treasureCollector 	= new FakeTreasureCollector();
		robot 				= new Robot(0, 0,
								1 /*start Zone*/,
								sensors,  
								robotCommander, 
								null/*maze*/, 
								solver/*solver*/, 
								colorDetectors, 
								treasureCollector);		
		unloader 			= new Unloader(robot);
	}
	
	@Override
	public void tearDown() {

	}
	
	public void testUnloaderState() {
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.START);
		unloader.PerformAction();
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.LOCATE_MAZE);
		
		sensors.setFrontWall(true);
		maze3Detector.m_present = true;
		loopUntilCondition(unloader, UnloaderState.ALIGN_ROBOT_WITH_MAZE);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.ALIGN_ROBOT_WITH_MAZE);
		
		loopUntilCondition(unloader, UnloaderState.LOCATE_ENTRANCE);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.LOCATE_ENTRANCE);
		
		loopUntilCondition(unloader, UnloaderState.LOAD_MAZE_TEMPLATE);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.LOAD_MAZE_TEMPLATE);
		
		loopUntilCondition(unloader, UnloaderState.UNLOAD);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.UNLOAD);
		
		sensors.setFrontWall(false);
		sensors.setLeftWallOnTimeout(5);
		sensors.setRightWallOnTimeout(5);
		loopUntilCondition(unloader, UnloaderState.MAP_MAZE_LOCATION);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.MAP_MAZE_LOCATION);
		
		loopUntilCondition(unloader, UnloaderState.START);
		Assert.assertEquals(unloader.getStateMachine(), UnloaderState.START);
	}
}
